package com.app;

import com.app.common.MoveMouseJS;
import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import com.app.common.MarkdownUtil;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Author:King
 * Create:2024/7/29 22:09
 */
public class TestAppController {
    @FXML
    private WebView markdownArea;

    @FXML
    private void initialize() {
        WebEngine webEngine = markdownArea.getEngine();
        // 添加用户样式
        //outputWebView.getEngine().setUserStyleSheetLocation(getClass().getResource("css/colour-messages.css").toExternalForm());

//        WebEngine webEngine = outputWebView.getEngine();

        //updateWebView();
        // 设置HTML内容
        String htmlContent = "<html><body contenteditable='true'></body></html>";
        // 加载HTML内容
        webEngine.loadContent(htmlContent);

        // 在页面加载完成后执行JavaScript
        webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                //markdownArea.setPrefHeight((Integer) webEngine.executeScript("document.body.scrollHeight"));
                //MoveMouseJS.moveCursorToEnd(webEngine);
                //MoveMouseJS.test(webEngine);
                //MoveMouseJS.addClickEventListener(webEngine);
            }
        });
        // 初始 load 本地文件
//        initLoad();


        // 设置VBox的宽度为50%
//        double halfWidth = mainBorderPane.getWidth() / 2;
//        inputVBox.setPrefWidth(halfWidth);
        //inputVBox.setPrefHeight(mainBorderPane.getHeight());
        //inputTextArea.setMaxHeight(inputVBox.getHeight());

        // 监听窗口尺寸变化
//        mainBorderPane.widthProperty().addListener(new ChangeListener<Number>() {
//            @Override
//            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
//                double halfWidth = newValue.doubleValue() / 2;
//                inputVBox.setPrefWidth(halfWidth);
//            }
//        });

    }

    @FXML
    private void handleSave() {
        try (FileWriter writer = new FileWriter("note.md")) {
            writer.write(markdownArea.getAccessibleText());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void handleLoad() {
        try (BufferedReader reader = new BufferedReader(new FileReader("note.md"))) {
            StringBuilder content = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("line = " + line);
                content.append(line).append("\n");
            }
//            markdownArea.setText(content.toString());
//            previewArea.getEngine().loadContent(markdownToHtml(content.toString()));
            markdownArea.setAccessibleText(MarkdownUtil.parseMarkdownToHtml(content.toString()));
            //previewArea.getCssMetaData()
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void updateWebView() {
        String markdownText = markdownArea.getAccessibleText();
        System.out.println("输入 = " + markdownText);
        String htmlContent = MarkdownUtil.parseMarkdownToHtml(markdownText);
        System.out.println("输入的转为html = " + htmlContent);
        markdownArea.getEngine().loadContent(htmlContent);
    }
}
